package com.suning.utils;

import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author 16080951(林健)
 * @Copyright (c) 2016 苏宁软件技术有限公司 版权所有
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class EsUtils {

    //IP地址。
    private final static String IP = "10.19.41.221";

    //端口
    private final static int PORT = 9301;

    //客户端
    private static Client CLIENT = null;

    public static Client openConnection(){
        try {
            if(CLIENT !=null) return CLIENT;
            Settings settings = Settings.builder().put("cluster.name", "es5").build();
            CLIENT = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(IP), PORT));

        } catch (UnknownHostException e) {
            e.printStackTrace();
            System.out.println("get Client Exception" + e.fillInStackTrace());
        }
        return CLIENT;
    }

    public static void closeConnection(){
        CLIENT.close();
    }
}
